﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public; 

namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietDonViTinh : DevExpress.XtraEditors.XtraForm, IChildForm
    {
        DonViTinh_BUS dvt_bus = new DonViTinh_BUS();
        public ChiTietDonViTinh()
        {
            InitializeComponent();
        }

        private void ChiTietDonViTinh_Load(object sender, EventArgs e)
        {
            Nap();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
        }
        public void Nap()
        {
            gridDonViTinh.DataSource = dvt_bus.LayDSDVT();
        }
        public void Them()
        {
            DonViTinh dvt = new DonViTinh();
            dvt.Tag = this;
            dvt.ShowDialog();
            Nap();
        }

        public void Sua()
        {
            // lay gia tri dang chọn tren grd moi dung

            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;

            DonViTinh dvt = new DonViTinh(r["MADONVITINH"].ToString(), r["DIENGIAI"].ToString());
            
            dvt.Tag = this;
            dvt.ShowDialog();
            Nap();
        }
        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa đơn vị tính <{0}>?"
                , r["MADONVITINH"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                dvt_bus.XoaDonViTinh(r["MADONVITINH"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }
    }
}